package com.example.weatherforecast

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.widget.ViewPager2
import com.example.weatherforecast.whtrcdn.WhtrcdnViewModel
import com.example.weatherforecast.whtrcdn.activity.WeatherAboutActivity
import com.example.weatherforecast.whtrcdn.activity.WeatherCityManagerActivity
import com.example.weatherforecast.whtrcdn.adapter.WeatherInfoViewPagerAdapter
import com.example.weatherforecast.whtrcdn.fragment.WeatherInfoFragment
import com.rd.PageIndicatorView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val TAG = MainActivity::class.java.simpleName
    private var viewModel: WhtrcdnViewModel ?= null
    private var currentCityIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        viewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(application)).get(WhtrcdnViewModel::class.java)

        // 主界面
        vp_weather_info_list.adapter = WeatherInfoViewPagerAdapter(this, getWeatherInfoFragmentList())
        val viewPagerIndicator: PageIndicatorView = findViewById(R.id.piv_weather)

        viewPagerIndicator.count = getWeatherInfoFragmentList().size
        viewPagerIndicator.selection = 0

        vp_weather_info_list.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                viewPagerIndicator.selection = position
            }
        })

        // 初始化城市数据
        if (viewModel?.getAppInit() == 0){
            Log.i(TAG, "onCreate: " + "init city data")

            viewModel?.setAppInit(1)
            // 从网络获取城市数据
//            viewModel!!.cityRequestStatus.observe(this, {
//                Log.i(TAG, "city data status: $it")
//                when(it){
//                    1 -> {
//                        viewModel?.initCityLevelTwoData()
//                    }
//                    2 -> {
//                        viewModel?.initCityLevelThreeData()
//                    }
//                    else -> {
//                        return@observe
//                    }
//                }
//            })
            viewModel?.clearCityTableData()
            // 从json文件得到城市数据
            viewModel?.importCityDataFromJsonFile()

            // viewModel?.initCityLevelOneData()

            Toast.makeText(this, "初始化城市数据导入成功", Toast.LENGTH_SHORT).show()
        }else {
            Log.i(TAG, "city data exist")
        }

        //  导出城市数据
        //viewModel?.exportCityDataToJsonFile()
        // 解析xml数据
        //viewModel?.parseXmlData()

    }

    private fun getWeatherInfoFragmentList(): List<WeatherInfoFragment> {
        val weatherInfoFragmentList = ArrayList<WeatherInfoFragment>()
        viewModel?.getCurrentWeatherCityInfoList()?.forEachIndexed { index, weatherCityInfo ->
            if (weatherCityInfo.defaultTag == 1){
                currentCityIndex = index
            }
            weatherInfoFragmentList.add(WeatherInfoFragment(R.layout.fragment_whtrcdn_main_layout_list, weatherCityInfo.cityId))
        }
        vp_weather_info_list.setCurrentItem(currentCityIndex, false)
        return weatherInfoFragmentList
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        MenuInflater(this).inflate(R.menu.menu_weather_main, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        when(item.itemId){
            R.id.menu_item_weather_city_manager -> {
                startActivity(Intent(this, WeatherCityManagerActivity::class.java))
            }
//            R.id.menu_item_weather_setting -> {
//                startActivity(Intent(this, WeatherSettingActivity::class.java))
//            }
            R.id.menu_item_weather_about -> {
                startActivity(Intent(this, WeatherAboutActivity::class.java))
            }
        }
        return super.onOptionsItemSelected(item)

    }

}